// pages/new-index/new-index.js
import {
  COS_HOST,
  httpServer
} from "../../utils/httpServer";

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 自定义导航栏
    navBarHeight: app.globalData.navBarHeight,
    menuRight: app.globalData.menuRight,
    menuBottom: app.globalData.menuBottom,
    menuHeight: app.globalData.menuHeight,

    COS_HOST,

    currentSwip: 0,
    autoplay: false,
    duration: 500,
    interval: 5000,
    banner: [],
    swiperList: [], // banner img
    actId: 0,
    contentList: [],
  },

  getQueryParam(url, param) {
    // 使用正则表达式构造匹配模式
    const regex = new RegExp('[?&]' + param + '=([^&#]*)', 'i');
    const result = regex.exec(url);
    // 返回解码后的参数值，如果未找到则返回 null
    return result ? decodeURIComponent(result[1]) : null;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(query) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getHomePage();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  onSwipeChange(e){
    this.setData({
      currentSwip: e.detail.current,
    })
  },

  onBannerTap(e) {
    const { index } = e.detail;
    const target = this.data.banner[index];
    if (target.type === 1) {
      wx.navigateTo({
        url: `/pages/news-detail/news-detail?id=${target.rel_id}`,
      })
    } else if (target.type === 2) {
      wx.navigateToMiniProgram({
        appId: target.app_id,
        path: '/pages/home/home',
      })
    }
  },

  onItemTap(e) {
    const { item } = e.currentTarget.dataset;
    if (item.type === 1) {
      wx.navigateTo({
        url: `/pages/news-detail/news-detail?id=${item.rel_id}`,
      })
    } else if (item.type === 2) {
      wx.navigateToMiniProgram({
        appId: item.app_id,
        path: '/pages/home/home',
      })
    }
  },

  getHomePage() {
    httpServer('POST', 'Home', 'GetHomePage')
      .then(res => {
        if (res.result == 'success') {
          const swiperList = [];
          res.info.banner.map(item => {
            swiperList.push(COS_HOST + item.thumb + '?imageMogr2/thumbnail/750x');
            return item;
          });
          this.setData({
            actId: res.info.act_id,
            swiperList,
            banner: res.info.banner,
            contentList: res.info.content
          });
        }
      })
      .catch(err => {
        console.error(err);
      })
  },

  goActivity() {
    if (this.data.actId == -1) {
      wx.showToast({
        icon: 'none',
        title: '当前暂无活动',
      })
      return;
    }

    wx.navigateTo({
      url: `/pages/activity-sign-up/activity-sign-up?id=${this.data.actId}`
    })
  },

  goShow() {
    wx.navigateTo({
      url: '/pages/show/show',
    })
  },

  goShop() {
    wx.navigateTo({
      url: '/pages/shop/shop',
    })
  }
})